# -*- coding: utf-8 -*- 

import wx
import wx.xrc
import Database
import Model
import os
from datetime import *
from sqlobject import *
import codecs

###########################################################################
## Class ProductTypeDiglog
###########################################################################

class ProductTypeDialog ( wx.Dialog ):
    
    def __init__( self, parent ):
        wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"产品类型", pos = wx.DefaultPosition, size = wx.Size( 600,420 ), style = wx.DEFAULT_DIALOG_STYLE )
        
        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        
        bSizer34 = wx.BoxSizer( wx.VERTICAL )
        
        self.m_lcList = wx.ListCtrl( self, wx.ID_ANY, wx.DefaultPosition, (-1, 140), wx.LC_REPORT )
        self.m_lcList.InsertColumn(0, u"编号")
        self.m_lcList.InsertColumn(1, u"名称")
        self.m_lcList.InsertColumn(2, u"中文名")
        self.m_lcList.InsertColumn(3, u'条码')
        self.m_lcList.InsertColumn(4, u"分类")
        self.m_lcList.InsertColumn(5, u"左前束最小值")
        self.m_lcList.InsertColumn(6, u"左前束最大值")
        self.m_lcList.InsertColumn(7, u'左外倾最小值')
        self.m_lcList.InsertColumn(8, u'左外倾最大值')
        self.m_lcList.InsertColumn(9, u"右前束最小值")
        self.m_lcList.InsertColumn(10, u"右前束最大值")
        self.m_lcList.InsertColumn(11, u'右外倾最小值')
        self.m_lcList.InsertColumn(12, u'右外倾最大值')
        bSizer34.Add( self.m_lcList, 1, wx.ALL|wx.EXPAND, 5 )
        
        self.m_staticline20 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
        bSizer34.Add( self.m_staticline20, 0, wx.EXPAND |wx.ALL, 5 )
        
        bSizer35 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_btnNew = wx.Button( self, wx.ID_ANY, u"新建", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer35.Add( self.m_btnNew, 0, wx.ALL, 5 )
        
        self.m_btnDelete = wx.Button( self, wx.ID_ANY, u"删除", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer35.Add( self.m_btnDelete, 0, wx.ALL, 5 )
        
        self.m_btnEdit = wx.Button( self, wx.ID_ANY, u"修改", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer35.Add( self.m_btnEdit, 0, wx.ALL, 5 )
        
        self.m_btnSubmit = wx.Button( self, wx.ID_ANY, u"提交", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer35.Add( self.m_btnSubmit, 0, wx.ALL, 5 )
        
        bSizer34.Add( bSizer35, 0, wx.EXPAND, 5 )
        
        sbSizer8 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"内容" ), wx.VERTICAL )
        
        fgSizer11 = wx.FlexGridSizer( 0, 8, 0, 0 )
        fgSizer11.SetFlexibleDirection( wx.BOTH )
        fgSizer11.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
        
        self.m_staticText54 = wx.StaticText( self, wx.ID_ANY, u"编号", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText54.Wrap( -1 )
        fgSizer11.Add( self.m_staticText54, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_tcNo = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcNo.Enable( False )
        
        fgSizer11.Add( self.m_tcNo, 0, wx.ALL, 5 )
        
        self.m_staticText53 = wx.StaticText( self, wx.ID_ANY, u"名称", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText53.Wrap( -1 )
        fgSizer11.Add( self.m_staticText53, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_tcName = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcName.Enable( False )
        
        fgSizer11.Add( self.m_tcName, 0, wx.ALL, 5 )
        
        self.m_staticText55 = wx.StaticText( self, wx.ID_ANY, u"中文名", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText55.Wrap( -1 )
        fgSizer11.Add( self.m_staticText55, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_tcChiese = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcChiese.Enable( False )
        
        fgSizer11.Add( self.m_tcChiese, 0, wx.ALL, 5 )
        
        self.m_staticText550 = wx.StaticText( self, wx.ID_ANY, u"条码", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText550.Wrap( -1 )
        fgSizer11.Add( self.m_staticText550, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_tcBarcode = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcBarcode.Enable( False )
        
        fgSizer11.Add( self.m_tcBarcode, 0, wx.ALL, 5 )
        
        self.m_staticText56 = wx.StaticText( self, wx.ID_ANY, u"分类", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText56.Wrap( -1 )
        fgSizer11.Add( self.m_staticText56, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        m_chCategoryChoices = Model.MPartCategory.GetNames()
        self.m_chCategory = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.Size( 100,-1 ), m_chCategoryChoices, 0 )
        self.m_chCategory.SetSelection( 0 )
        self.m_chCategory.Enable( False )
        
        fgSizer11.Add( self.m_chCategory, 0, wx.ALL, 5 )
        
        fgSizer11.Add((0, 0), 0, wx.ALL, 5)
        fgSizer11.Add((0, 0), 0, wx.ALL, 5)
        fgSizer11.Add((0, 0), 0, wx.ALL, 5)
        fgSizer11.Add((0, 0), 0, wx.ALL, 5)
        fgSizer11.Add((0, 0), 0, wx.ALL, 5)
        fgSizer11.Add((0, 0), 0, wx.ALL, 5)
        
        self.m_staticText57 = wx.StaticText( self, wx.ID_ANY, u"左前束最小值", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText57.Wrap( -1 )
        fgSizer11.Add( self.m_staticText57, 0, wx.ALL, 5 )
        
        self.m_tcLConsAngle = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcLConsAngle.Enable( False )
        
        fgSizer11.Add( self.m_tcLConsAngle, 0, wx.ALL, 5 )
        
        self.m_staticText58 = wx.StaticText( self, wx.ID_ANY, u"左前束最大值", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText58.Wrap( -1 )
        fgSizer11.Add( self.m_staticText58, 0, wx.ALL, 5 )
        
        self.m_tcLConsAngleToler = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcLConsAngleToler.Enable( False )
        
        fgSizer11.Add( self.m_tcLConsAngleToler, 0, wx.ALL, 5 )
        
        self.m_staticText59 = wx.StaticText( self, wx.ID_ANY, u"左外倾最小值", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText59.Wrap( -1 )
        fgSizer11.Add( self.m_staticText59, 0, wx.ALL, 5 )
        
        self.m_tcLCambAngle = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcLCambAngle.Enable( False )
        
        fgSizer11.Add( self.m_tcLCambAngle, 0, wx.ALL, 5 )
        
        self.m_staticText60 = wx.StaticText( self, wx.ID_ANY, u"左倾最大值", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText60.Wrap( -1 )
        fgSizer11.Add( self.m_staticText60, 0, wx.ALL, 5 )
        
        self.m_tcLCambAngleToler = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcLCambAngleToler.Enable( False )
        
        fgSizer11.Add( self.m_tcLCambAngleToler, 0, wx.ALL, 5 )
        
        self.m_staticText61 = wx.StaticText( self, wx.ID_ANY, u"右前束最小值", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText61.Wrap( -1 )
        fgSizer11.Add( self.m_staticText61, 0, wx.ALL, 5 )
        
        self.m_tcRConsAngle = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcRConsAngle.Enable( False )
        
        fgSizer11.Add( self.m_tcRConsAngle, 0, wx.ALL, 5 )
        
        self.m_staticText62 = wx.StaticText( self, wx.ID_ANY, u"右前束最大值", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText62.Wrap( -1 )
        fgSizer11.Add( self.m_staticText62, 0, wx.ALL, 5 )
        
        self.m_tcRConsAngleToler = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcRConsAngleToler.Enable( False )
        
        fgSizer11.Add( self.m_tcRConsAngleToler, 0, wx.ALL, 5 )
        
        self.m_staticText63 = wx.StaticText( self, wx.ID_ANY, u"右外倾最小值", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText63.Wrap( -1 )
        fgSizer11.Add( self.m_staticText63, 0, wx.ALL, 5 )
        
        self.m_tcRCambAngle = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcRCambAngle.Enable( False )
        
        fgSizer11.Add( self.m_tcRCambAngle, 0, wx.ALL, 5 )
        
        self.m_staticText64 = wx.StaticText( self, wx.ID_ANY, u"右外倾最大值", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText64.Wrap( -1 )
        fgSizer11.Add( self.m_staticText64, 0, wx.ALL, 5 )
        
        self.m_tcRCambAngleToler = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcRCambAngleToler.Enable( False )
        
        fgSizer11.Add( self.m_tcRCambAngleToler, 0, wx.ALL, 5 )
        
        sbSizer8.Add( fgSizer11, 1, wx.EXPAND, 5 )
        
        bSizer34.Add( sbSizer8, 0, wx.EXPAND, 5 )
        
        self.m_staticline21 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
        bSizer34.Add( self.m_staticline21, 0, wx.EXPAND |wx.ALL, 5 )
        
        m_sdbSizer8 = wx.StdDialogButtonSizer()
        self.m_sdbSizer8OK = wx.Button( self, wx.ID_OK, label = u"关闭" )
        m_sdbSizer8.AddButton( self.m_sdbSizer8OK )
        m_sdbSizer8.Realize();
        bSizer34.Add( m_sdbSizer8, 0, wx.EXPAND, 5 )
        
        self.SetSizer( bSizer34 )
        self.Layout()
        
        self.Centre( wx.BOTH )
        self.Fit()
        
        # Connect Events
        self.m_lcList.Bind( wx.EVT_LIST_ITEM_SELECTED, self.OnListItemSelected )
        self.m_btnNew.Bind( wx.EVT_BUTTON, self.OnNew )
        self.m_btnDelete.Bind( wx.EVT_BUTTON, self.OnDelete )
        self.m_btnEdit.Bind( wx.EVT_BUTTON, self.OnEdit )
        self.m_btnSubmit.Bind( wx.EVT_BUTTON, self.OnSubmit )
        
        self.currentitem = None
        self.dict = {}
        self._Init()
    
    
    # Virtual event handlers, overide them in your derived class
    def OnListItemSelected( self, event ):
        self.currentitem = item = event.m_itemIndex
        self.m_tcNo.SetValue(self.m_lcList.GetItemText(item))
        self.m_tcName.SetValue(self.m_lcList.GetItem(item, 1).GetText())
        self.m_tcChiese.SetValue(self.m_lcList.GetItem(item, 2).GetText())
        self.m_tcBarcode.SetValue(self.m_lcList.GetItem(item, 3).GetText())
        self.m_chCategory.SetStringSelection(self.m_lcList.GetItem(item, 4).GetText())
        self.m_tcLConsAngle.SetValue(self.m_lcList.GetItem(item, 5).GetText())
        self.m_tcLConsAngleToler.SetValue(self.m_lcList.GetItem(item, 6).GetText())
        self.m_tcLCambAngle.SetValue(self.m_lcList.GetItem(item, 7).GetText())
        self.m_tcLCambAngleToler.SetValue(self.m_lcList.GetItem(item, 8).GetText())
        self.m_tcRConsAngle.SetValue(self.m_lcList.GetItem(item, 9).GetText())
        self.m_tcRConsAngleToler.SetValue(self.m_lcList.GetItem(item, 10).GetText())
        self.m_tcRCambAngle.SetValue(self.m_lcList.GetItem(item, 11).GetText())
        self.m_tcRCambAngleToler.SetValue(self.m_lcList.GetItem(item, 12).GetText())
        event.Skip()
    
    def OnNew( self, event ):
        self.m_tcNo.SetValue(u"")
        self.m_tcName.SetValue(u"")
        self.m_tcName.Enable(True)
        self.m_tcChiese.SetValue(u"")
        self.m_tcChiese.Enable(True)
        self.m_tcBarcode.SetValue(u'')
        self.m_tcBarcode.Enable(True)
        self.m_chCategory.Enable(True)
        self.m_tcLConsAngle.SetValue(u"")
        self.m_tcLConsAngle.Enable(True)
        self.m_tcLConsAngleToler.SetValue(u"")
        self.m_tcLConsAngleToler.Enable(True)
        self.m_tcLCambAngle.SetValue(u"")
        self.m_tcLCambAngle.Enable(True)
        self.m_tcLCambAngleToler.SetValue(u"")
        self.m_tcLCambAngleToler.Enable(True)
        self.m_tcRConsAngle.SetValue(u"")
        self.m_tcRConsAngle.Enable(True)
        self.m_tcRConsAngleToler.SetValue(u"")
        self.m_tcRConsAngleToler.Enable(True)
        self.m_tcRCambAngle.SetValue(u"")
        self.m_tcRCambAngle.Enable(True)
        self.m_tcRCambAngleToler.SetValue(u"")
        self.m_tcRCambAngleToler.Enable(True)
        event.Skip()
    
    def OnDelete( self, event ):
        if self.currentitem == None:
            wx.MessageBox(u"请选择需要删除的条目")
            return
        
        if wx.MessageBox(u"你确定需要删除吗？一旦删除您将不能恢复", u"提示", wx.YES_NO) == wx.YES:
            index = int(self.m_tcNo.GetValue())
            Database.MUser.deleteBy(id = index)
            self.m_lcList.DeleteItem(self.currentitem)
            del self.dict[index]
            self.currentitem = None
            
            self.m_tcNo.SetValue(u"")
            self.m_tcName.SetValue(u"")
            self.m_tcName.Enable(False)
            self.m_tcChiese.SetValue(u"")
            self.m_tcChiese.Enable(False)
            self.m_tcBarcode.SetValue(u"")
            self.m_tcBarcode.Enable(False)
            self.m_chCategory.Enable(False)
            self.m_tcLConsAngle.SetValue(u"")
            self.m_tcLConsAngle.Enable(False)
            self.m_tcLConsAngleToler.SetValue(u"")
            self.m_tcLConsAngleToler.Enable(False)
            self.m_tcLCambAngle.SetValue(u"")
            self.m_tcLCambAngle.Enable(False)
            self.m_tcLCambAngleToler.SetValue(u"")
            self.m_tcLCambAngleToler.Enable(False)
            self.m_tcRConsAngle.SetValue(u"")
            self.m_tcRConsAngle.Enable(False)
            self.m_tcRConsAngleToler.SetValue(u"")
            self.m_tcRConsAngleToler.Enable(False)
            self.m_tcRCambAngle.SetValue(u"")
            self.m_tcRCambAngle.Enable(False)
            self.m_tcRCambAngleToler.SetValue(u"")
            self.m_tcRCambAngleToler.Enable(False)
        event.Skip()
    
    def OnEdit( self, event ):
        if self.currentitem == None:
            wx.MessageBox(u"请选择需要删除的条目")
            return
        
        self.m_tcName.Enable(True)
        self.m_tcChiese.Enable(True)
        self.m_tcBarcode.Enable(True)
        self.m_chCategory.Enable(True)
        self.m_tcLConsAngle.Enable(True)
        self.m_tcLConsAngleToler.Enable(True)
        self.m_tcLCambAngle.Enable(True)
        self.m_tcLCambAngleToler.Enable(True)
        self.m_tcRConsAngle.Enable(True)
        self.m_tcRConsAngleToler.Enable(True)
        self.m_tcRCambAngle.Enable(True)
        self.m_tcRCambAngleToler.Enable(True)
        event.Skip()
    
    def OnSubmit( self, event ):
        if self.m_tcNo.GetValue() == wx.EmptyString:
            r = Database.MPartType(name = self.m_tcName.GetValue(),
                                  chinese = self.m_tcChiese.GetValue(),
                                  barcode = self.m_tcBarcode.GetValue(),
                                  category = Model.MPartCategory.Find(self.m_chCategory.GetStringSelection()),
                                  lstdconsangle = float(self.m_tcLConsAngle.GetValue()),
                                  lconsangletoler = float(self.m_tcLConsAngleToler.GetValue()),
                                  lstdcambangle = float(self.m_tcLCambAngle.GetValue()),
                                  lcambangletoler = float(self.m_tcLCambAngleToler.GetValue()),
                                  rstdconsangle = float(self.m_tcRConsAngle.GetValue()),
                                  rconsangletoler = float(self.m_tcRConsAngleToler.GetValue()),
                                  rstdcambangle = float(self.m_tcRCambAngle.GetValue()),
                                  rcambangletoler = float(self.m_tcRCambAngleToler.GetValue()))
            self._InsertItem(r)
        else:
            r = Database.MPartType.get(id = int(self.m_tcNo.GetValue()))
            r.name = self.m_tcName.GetValue().encode('ascii')
            r.chinese = self.m_tcChiese.GetValue()
            r.barcode = self.m_tcBarcode.GetValue()
            r.category = Model.MPartCategory.Find(self.m_chCategory.GetStringSelection())
            r.lstdconsangle = float(self.m_tcLConsAngle.GetValue())
            r.lconsangletoler = float(self.m_tcLConsAngleToler.GetValue())
            r.lstdcambangle = float(self.m_tcLCambAngle.GetValue())
            r.lcambangletoler = float(self.m_tcLCambAngleToler.GetValue())
            r.rstdconsangle = float(self.m_tcRConsAngle.GetValue())
            r.rconsangletoler = float(self.m_tcRConsAngleToler.GetValue())
            r.rstdcambangle = float(self.m_tcRCambAngle.GetValue())
            r.rcambangletoler = float(self.m_tcRCambAngleToler.GetValue())
            
            self._UpdateItem(r)
            
        self.m_tcName.Enable(False)
        self.m_tcChiese.Enable(False)
        self.m_tcBarcode.Enable(True)
        self.m_chCategory.Enable(False)
        self.m_tcLConsAngle.Enable(False)
        self.m_tcLConsAngleToler.Enable(False)
        self.m_tcLCambAngle.Enable(False)
        self.m_tcLCambAngleToler.Enable(False)
        self.m_tcRConsAngle.Enable(False)
        self.m_tcRConsAngleToler.Enable(False)
        self.m_tcRCambAngle.Enable(False)
        self.m_tcRCambAngleToler.Enable(False)
        event.Skip()
    
    def _Init(self):
        res = Database.MPartType.select()
        for r in res:
            self._InsertItem(r)
    
    def _InsertItem(self, r):
        item = self.m_lcList.GetItemCount()
        self.m_lcList.InsertStringItem(item, '%d' % r.id)
        self.m_lcList.SetStringItem(item, 1, r.name)
        try:
            self.m_lcList.SetStringItem(item, 2, r.chinese)
        except:
            pass
        try:
            self.m_lcList.SetStringItem(item, 3, r.barcode)
        except:
            self.m_lcList.SetStringItem(item, 3, u"")
        self.m_lcList.SetStringItem(item, 4, r.category.name)
        self.m_lcList.SetStringItem(item, 5, u'%.3f' % r.lstdconsangle)
        self.m_lcList.SetStringItem(item, 6, '%.3f' % r.lconsangletoler)
        self.m_lcList.SetStringItem(item, 7, u'%.3f' % r.lstdcambangle)
        self.m_lcList.SetStringItem(item, 8, '%.3f' % r.lcambangletoler)
        self.m_lcList.SetStringItem(item, 9, u'%.3f' % r.rstdconsangle)
        self.m_lcList.SetStringItem(item, 10, '%.3f' % r.rconsangletoler)
        self.m_lcList.SetStringItem(item, 11, u'%.3f' % r.rstdcambangle)
        self.m_lcList.SetStringItem(item, 12, '%.3f' % r.rcambangletoler)
        self.dict[r.id] = item

    def _UpdateItem(self, r):
        item = self.dict[r.id]
        self.m_lcList.SetStringItem(item, 1, r.name)
        try:
            self.m_lcList.SetStringItem(item, 2, r.chinese)
        except:
            self.m_lcList.SetStringItem(item, 2, u"")
        try:
            self.m_lcList.SetStringItem(item, 3, r.barcode)
        except:
            self.m_lcList.SetStringItem(item, 3, u"")
        self.m_lcList.SetStringItem(item, 4, r.category.name)
        self.m_lcList.SetStringItem(item, 5, u'%.3f' % r.lstdconsangle)
        self.m_lcList.SetStringItem(item, 6, '%.3f' % r.lconsangletoler)
        self.m_lcList.SetStringItem(item, 7, u'%.3f' % r.lstdcambangle)
        self.m_lcList.SetStringItem(item, 8, '%.3f' % r.lcambangletoler)
        self.m_lcList.SetStringItem(item, 9, u'%.3f' % r.rstdconsangle)
        self.m_lcList.SetStringItem(item, 10, '%.3f' % r.rconsangletoler)
        self.m_lcList.SetStringItem(item, 11, u'%.3f' % r.rstdcambangle)
        self.m_lcList.SetStringItem(item, 12, '%.3f' % r.rcambangletoler)
        
        
class PartQueryDialog ( wx.Dialog ):
    
    def __init__( self, parent ):
        wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"产品查询", pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_DIALOG_STYLE )
        
        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        
        bSizer8 = wx.BoxSizer( wx.VERTICAL )
        
        bSizer9 = wx.BoxSizer( wx.HORIZONTAL )
        
        sbSizer1 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"型号选择" ), wx.VERTICAL )
        
        self.m_clPartTypeChoices = []
        self.m_clPartType = wx.CheckListBox( self, wx.ID_ANY, wx.DefaultPosition, wx.Size( -1,100 ), self.m_clPartTypeChoices, 0 )
        sbSizer1.Add( self.m_clPartType, 1, wx.ALL, 5 )
        
        bSizer9.Add( sbSizer1, 0, wx.ALL|wx.EXPAND, 5 )
        
        sbSizer2 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"查询方式" ), wx.VERTICAL )
        
        bSizer10 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_rbDateQuery = wx.RadioButton( self, wx.ID_ANY, u"日期查询", wx.DefaultPosition, wx.DefaultSize, wx.RB_GROUP )
        self.m_rbDateQuery.SetValue( True ) 
        bSizer10.Add( self.m_rbDateQuery, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_staticline5 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_VERTICAL )
        bSizer10.Add( self.m_staticline5, 0, wx.EXPAND |wx.ALL, 5 )
        
        self.m_dpBeginDate = wx.DatePickerCtrl( self, wx.ID_ANY, wx.DefaultDateTime, wx.DefaultPosition, wx.DefaultSize, wx.DP_DEFAULT )
        bSizer10.Add( self.m_dpBeginDate, 0, wx.ALL, 5 )
        
        self.m_staticText21 = wx.StaticText( self, wx.ID_ANY, u"到", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText21.Wrap( -1 )
        bSizer10.Add( self.m_staticText21, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_dpEndDate = wx.DatePickerCtrl( self, wx.ID_ANY, wx.DefaultDateTime, wx.DefaultPosition, wx.DefaultSize, wx.DP_DEFAULT )
        bSizer10.Add( self.m_dpEndDate, 0, wx.ALL, 5 )
        
        sbSizer2.Add( bSizer10, 0, wx.EXPAND, 5 )
        
        bSizer11 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_rbLastQuery = wx.RadioButton( self, wx.ID_ANY, u"最近记录", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer11.Add( self.m_rbLastQuery, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_staticline6 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_VERTICAL )
        bSizer11.Add( self.m_staticline6, 0, wx.EXPAND |wx.ALL, 5 )
        
        m_cbLastCountChoices = [ u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"10", u"15", u"20", u"30" ]
        self.m_cbLastCount = wx.ComboBox( self, wx.ID_ANY, u"1", wx.DefaultPosition, wx.Size( 100,-1 ), m_cbLastCountChoices, 0 )
        bSizer11.Add( self.m_cbLastCount, 0, wx.ALL, 5 )
        
        sbSizer2.Add( bSizer11, 0, wx.EXPAND, 5 )
        
        bSizer121 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_rbAdvQuery = wx.RadioButton( self, wx.ID_ANY, u"条码查询", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer121.Add( self.m_rbAdvQuery, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_staticline71 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_VERTICAL )
        bSizer121.Add( self.m_staticline71, 0, wx.EXPAND |wx.ALL, 5 )
        
        self.m_tcAdvSQL = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer121.Add( self.m_tcAdvSQL, 1, wx.ALL, 5 )
        
        sbSizer2.Add( bSizer121, 0, wx.EXPAND, 5 )
        
        bSizer9.Add( sbSizer2, 1, wx.EXPAND|wx.ALL, 5 )
        
        self.m_btnQuery = wx.Button( self, wx.ID_ANY, u"查询", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer9.Add( self.m_btnQuery, 0, wx.ALL|wx.ALIGN_BOTTOM, 5 )
        
        bSizer8.Add( bSizer9, 0, wx.EXPAND, 5 )
        
        self.m_staticline10 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
        bSizer8.Add( self.m_staticline10, 0, wx.EXPAND |wx.ALL, 5 )
        
        self.m_lcList = wx.ListCtrl( self, wx.ID_ANY, wx.DefaultPosition, wx.Size( -1,300 ), wx.LC_REPORT )
        bSizer8.Add( self.m_lcList, 1, wx.ALL|wx.EXPAND, 5 )
        
        bSizer16 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_btnExport = wx.Button( self, wx.ID_ANY, u"导出...", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer16.Add( self.m_btnExport, 0, wx.ALL, 5 )
        
        
        bSizer16.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
        
        self.m_btnClose = wx.Button( self, wx.ID_OK, u"关闭", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer16.Add( self.m_btnClose, 0, wx.ALL, 5 )
        
        bSizer8.Add( bSizer16, 0, wx.EXPAND, 5 )
        
        self.SetSizer( bSizer8 )
        self.Layout()
        bSizer8.Fit( self )
        
        self.Centre( wx.BOTH )
        
        self.Init()
        
        # Connect Events
        self.m_btnQuery.Bind( wx.EVT_BUTTON, self.OnQuery )
        self.m_btnExport.Bind( wx.EVT_BUTTON, self.OnExport)
    
    # Virtual event handlers, overide them in your derived class
    def OnQuery( self, event ):
        if self.m_rbDateQuery.GetValue():
            begindate = self.m_dpBeginDate.GetValue()
            enddate = self.m_dpEndDate.GetValue()
            begindate = datetime(begindate.GetYear(), 
                                      begindate.GetMonth() + 1,
                                      begindate.GetDay(),
                                      0,0,0)
            enddate = datetime(enddate.GetYear(), 
                                      enddate.GetMonth() + 1,
                                      enddate.GetDay(),
                                      23,59,59)
            r = Database.MPart.select(AND(Database.MPart.q.createtime > begindate, Database.MPart.q.createtime < enddate),
                           orderBy = DESC(Database.MPart.q.createtime))
            
            self.UpdateList(r)
        elif self.m_rbLastQuery.GetValue():
            try:
                cnt = int(self.m_cbLastCount.GetValue())
            except:
                wx.MessageBox(u"请输入正确的数据")
                return
            r = Database.MPart.select(orderBy = DESC(Database.MPart.q.createtime), limit = cnt)
            self.UpdateList(r)
        elif self.m_rbAdvQuery.GetValue():
            sql = self.m_tcAdvSQL.GetValue()
            r = Database.MPart.select("barcode ~* '%s'" % sql, orderBy = DESC(Database.MPart.q.createtime))
            self.UpdateList(r)
    
    def OnSPC( self, event ):
        event.Skip()
        
    def OnExport(self, event):
        
        wildcard = "CSV file (*.csv)|*.csv|" \
           "All files (*.*)|*.*"
        
        dlg = wx.FileDialog(
            self, message=u"导出 ...", defaultDir=os.path.join(os.getcwd(), 'export'), 
            defaultFile=datetime.now().strftime("%Y%m%d%H%M%S")+'.csv', wildcard=wildcard, style=wx.SAVE
            )
        if dlg.ShowModal() == wx.ID_OK:
            import csv
            writer = csv.writer(codecs.open(dlg.GetPath(), 'wb', 'utf-8'))
            rowcnt = self.m_lcList.GetItemCount()
            colcnt = self.m_lcList.GetColumnCount()
            
            vals = []
            for j in range(colcnt):
                vals.append(self.m_lcList.GetColumn(j).GetText())
            writer.writerow(vals)
            for i in range(rowcnt):
                vals = []
                for j in range(colcnt):
                    vals.append(self.m_lcList.GetItem(i, j).GetText())
                writer.writerow(vals)
        
    def Init(self):
        self.m_lcList.InsertColumn(0, u'ID')
        self.m_lcList.InsertColumn(1, u'产品型号')
        self.m_lcList.InsertColumn(2, u'产品条码')
        self.m_lcList.InsertColumn(3, u'创建时间')
        self.m_lcList.InsertColumn(4, u'开始时间')
        self.m_lcList.InsertColumn(5, u'结束时间')
        self.m_lcList.InsertColumn(6, u'左前束角')
        self.m_lcList.InsertColumn(7, u'左前束角是否合格')
        self.m_lcList.InsertColumn(8, u'左外倾角')
        self.m_lcList.InsertColumn(9, u'左外倾角是否合格')
        self.m_lcList.InsertColumn(10, u'右前束角')
        self.m_lcList.InsertColumn(11, u'右前束角是否合格')
        self.m_lcList.InsertColumn(12, u'右外倾角')
        self.m_lcList.InsertColumn(13, u'右外倾角是否合格')
        self.m_lcList.InsertColumn(14, u'扭矩1')
        self.m_lcList.InsertColumn(15, u'角度1')
        self.m_lcList.InsertColumn(16, u'拧紧是否合格1')
        self.m_lcList.InsertColumn(17, u'扭矩2')
        self.m_lcList.InsertColumn(18, u'角度2')
        self.m_lcList.InsertColumn(19, u'拧紧是否合格2')
        self.m_lcList.InsertColumn(20, u'扭矩3')
        self.m_lcList.InsertColumn(21, u'角度3')
        self.m_lcList.InsertColumn(22, u'拧紧是否合格3')
        self.m_lcList.InsertColumn(23, u'扭矩4')
        self.m_lcList.InsertColumn(24, u'角度4')
        self.m_lcList.InsertColumn(25, u'拧紧是否合格4')
        
        self.m_clPartTypeChoices = Model.MPartTypes.GetNames()
        self.m_clPartType.AppendItems(self.m_clPartTypeChoices)
        
    def UpdateList(self, record):
        self.m_lcList.DeleteAllItems()
        item = 0
        choices = self.m_clPartType.GetCheckedStrings()
        for i, vals in enumerate(record):
            if (vals.parttype and vals.parttype.name in choices) or (vals.parttype is None):
                self.m_lcList.InsertStringItem(item, str(vals.id))
                if not vals.parttype is None:
                    self.m_lcList.SetStringItem(item, 1, vals.parttype.name)
                if vals.barcode:
                    self.m_lcList.SetStringItem(item, 2, vals.barcode)
                self.m_lcList.SetStringItem(item, 3, vals.createtime.strftime("%Y-%m-%d %H:%M:%S"))
                self.m_lcList.SetStringItem(item, 4, vals.machinebegintime.strftime("%Y-%m-%d %H:%M:%S"))
                if vals.machineendtime:
                    self.m_lcList.SetStringItem(item, 5, vals.machineendtime.strftime("%Y-%m-%d %H:%M:%S"))
                self.m_lcList.SetStringItem(item, 6, u'%.3f' % vals.lconsangle)
                self.m_lcList.SetStringItem(item, 7, vals.lconsresult)
                self.m_lcList.SetStringItem(item, 8, u'%.3f' % vals.lcambangle)
                self.m_lcList.SetStringItem(item, 9, vals.lcambresult)
                self.m_lcList.SetStringItem(item, 10, u'%.3f' % vals.rconsangle)
                self.m_lcList.SetStringItem(item, 11, vals.rconsresult)
                self.m_lcList.SetStringItem(item, 12, u'%.3f' % vals.rcambangle)
                self.m_lcList.SetStringItem(item, 13, vals.rcambresult)
                if vals.torque1:
                    self.m_lcList.SetStringItem(item, 14, u'%.3f' % vals.torque1)
                if vals.angle1 != None:
                    self.m_lcList.SetStringItem(item, 15, u'%.3f' % vals.angle1)
                if vals.result1:
                    self.m_lcList.SetStringItem(item, 16, vals.result1)
                if vals.torque2:
                    self.m_lcList.SetStringItem(item, 17, u'%.3f' % vals.torque2)
                if vals.angle2 != None:
                    self.m_lcList.SetStringItem(item, 18, u'%.3f' % vals.angle2)
                if vals.result2:
                    self.m_lcList.SetStringItem(item, 19, vals.result2)
                    
                if vals.torque3:
                    self.m_lcList.SetStringItem(item, 20, u'%.3f' % vals.torque3)
                if vals.angle3 != None:
                    self.m_lcList.SetStringItem(item, 21, u'%.3f' % vals.angle3)
                if vals.result3:
                    self.m_lcList.SetStringItem(item, 22, vals.result3)
                    
                if vals.torque4:
                    self.m_lcList.SetStringItem(item, 23, u'%.3f' % vals.torque4)
                if vals.angle4 != None:
                    self.m_lcList.SetStringItem(item, 24, u'%.3f' % vals.angle4)
                if vals.result4:
                    self.m_lcList.SetStringItem(item, 25, vals.result4)
                item += 1
            